package com.gzp;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

public class TestListener {

	private static final String zkServers = "127.0.0.1:2181";

	private static ZkClient zkClient = null;
	static {
		// 超时时间
		int connectionTimeout = 5000;
		int sessionTimeout = 5000;
		zkClient = new ZkClient(zkServers, sessionTimeout, connectionTimeout, new CustomSerializer());
	}

	/**
	 * 监听线程
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		// 监听节点的变化
		zkClient.subscribeDataChanges("/test", new IZkDataListener() {

			/**
			 * 监听的节点被删除时调用
			 */
			public void handleDataDeleted(String dataPath) throws Exception {
				System.out.println(dataPath + "节点被删除！");
			}

			/**
			 * 监听的节点的值被修改时调用
			 */
			public void handleDataChange(String dataPath, Object data) throws Exception {
				System.out.println(dataPath + "节点的值被修改为" + data);
			}
		});
		System.out.println("监听启动成功！");
		System.in.read();
	}

}
